﻿using System;
using System.Web.Mvc;
using HiLand.Framework.BusinessCore;
using HiLand.Framework.BusinessCore.BLL;
using HiLand.Utility.Enums;
using HiLand.Utility4.MVC.Data;

namespace Legou365.Web.Controllers
{
    public class AccountController : Controller
    {
        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            if (BusinessUserBLL.IsLogined)
            {
                return RedirectToAction("Index", "Home");
            }

            this.BrokeParam("returnUrl");
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(BusinessUser model, string returnUrl="")
        {
            if (ModelState.IsValid)
            {
                LoginStatuses loginStatus=  LoginStatuses.Successful;
                BusinessUserBLL.Login(model.UserName, model.Password, out loginStatus);
                if (loginStatus== LoginStatuses.Successful)
                {
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "你输入的用户名和口令不正确.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult LogOff()
        {
            BusinessUserBLL.Logout();
            return RedirectToAction("Index", "Home");
        }

        //// **************************************
        //// URL: /Account/Register
        //// **************************************

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(BusinessUser model)
        {
            if (ModelState.IsValid)
            {
                CreateUserRoleStatuses createStatus= CreateUserRoleStatuses.Successful;
                
                //设置其他缺省属性
                model.UserRegisterDate = DateTime.Now;

                BusinessUserBLL.CreateUser(model, out createStatus);

                if (createStatus == CreateUserRoleStatuses.Successful)
                {
                    //FormsAuthentication.SetAuthCookie(model.UserName, false);
                    LoginStatuses loginStatus= LoginStatuses.Successful;
                    BusinessUserBLL.Login(model.UserName, model.Password, out loginStatus);
                    if (loginStatus == LoginStatuses.Successful)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    string errorInformation = string.Empty;
                    switch (createStatus)
                    {
                        case CreateUserRoleStatuses.FailureDuplicateEMail:
                            errorInformation = "对不起，你输入的Email在系统内已经存在";
                            break;
                        case CreateUserRoleStatuses.FailureDuplicateName:
                            errorInformation = "对不起，你输入的用户名在系统内已经存在";
                            break;
                        case CreateUserRoleStatuses.FailureUnknowReason:
                            errorInformation = "对不起，由于未知错误你本次注册没有成功";
                            break;
                        default:
                            break;
                    }
                    ModelState.AddModelError("", errorInformation);
                }
            }

            return View(model);
        }

        //// **************************************
        //// URL: /Account/ChangePassword
        //// **************************************

        //[Authorize]
        //public ActionResult ChangePassword()
        //{
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View();
        //}

        //[Authorize]
        //[HttpPost]
        //public ActionResult ChangePassword(ChangePasswordModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
        //        {
        //            return RedirectToAction("ChangePasswordSuccess");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View(model);
        //}

        //// **************************************
        //// URL: /Account/ChangePasswordSuccess
        //// **************************************

        //public ActionResult ChangePasswordSuccess()
        //{
        //    return View();
        //}

    }
}
